package com.aistar.controller;

import com.aistar.mapper.EmployeeMapper;
import com.aistar.mapper.UserMapper;
import com.aistar.model.bo.Result;
import com.aistar.model.dto.user.EmployeeCollectionAndCompamyAndRecruitmentDTO;
import com.aistar.model.dto.user.RecruitmentAndCompanyDTO;
import com.aistar.model.dto.user.ResumeDTO;
import com.aistar.model.dto.user.ResumeViewAndCompanyDTO;
import com.aistar.model.entity.*;
import com.aistar.service.user.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/user")
public class UserController {
    @GetMapping("/register")
    public String register() {//
        return "/WEB-INF/user/login/register.jsp";
    }

    @GetMapping("/register1")
    public String register1() {//注册
        return "/WEB-INF/user/login/register1.jsp";
    }

    @GetMapping("/login")
    public String logins() {
        return "/WEB-INF/user/login/login.jsp";
    }

    @PostMapping("/logincl")
    @ResponseBody
    public Result logincl(HttpServletRequest request, User user) {
//        System.out.println(user);
        HttpSession session = request.getSession();
        User u = userService.loginByPhone(user.getPhone());
//        System.out.println(u.getPassword());
//        System.out.println(user.getPassword());
        if (u == null)
            return new Result("404", "用户不存在");
        if (u.getPassword().equals(user.getPassword())) {
            session.setAttribute("user", u);
            return new Result("200", "登陆成功");
        }
        return new Result("500", "密码错误");
    }

    @Autowired
    private IResumeViewService resumeViewService;

    @Autowired
    private IEmployeeInfoService employeeInfoService;

    @GetMapping("/employee_info")
    public String employee_info(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Employee employee= (Employee) session.getAttribute("employee");
        request.setAttribute("employee",employee);
        User user = employeeInfoService.UserInfo(employee.getId());
        request.setAttribute("user",user);
//        System.out.println(user.getId());
        request.setAttribute("employee", employee);
//        System.out.println(employee);
        List<ResumeViewAndCompanyDTO> resumeViewList =resumeViewService.viewMe(employee.getId());
        request.setAttribute("resumeViewList",resumeViewList);
        request.setAttribute("count",resumeViewList.size());
        List<RecruitmentAndCompanyDTO> recruitmentAndCompanyDTOList = mydeliveryService.delivery(employee.getId());
        request.setAttribute("recruitmentAndCompanyDTOList",recruitmentAndCompanyDTOList);
        request.setAttribute("deliveryCount",recruitmentAndCompanyDTOList.size());
        ResumeDTO resumeNew = employeeInfoService.newResume(employee.getId());
        request.setAttribute("resumeNew",resumeNew);
        Date time = new Date();
        request.setAttribute("time",time);
        return "/WEB-INF/user/center/employee_info.jsp";
    }

    @Autowired
    private IUserService userService;

    @Autowired
    private JavaMailSender mailSender;

//    @GetMapping("/login")
//    public String login(){
//        return "user/login/login";
//    }

    @GetMapping("/send")
    @ResponseBody
    public Result send(HttpServletRequest request, String email) {
        User u = userService.login(email);
        if (u == null)
            return new Result("404", "邮箱不存在!");
        SimpleMailMessage msg = new SimpleMailMessage();

        msg.setFrom("2294014200@qq.com");
        msg.setSubject("欢迎您");//标题

        //验证码是随机的10000 ~ 99999
        int n = (int) (Math.random() * 90000 + 10000);

        //将这个验证码放入到session - 不能设置过期时间
        //以后放到redis,设置过期时间
        HttpSession session = request.getSession();
        session.setAttribute("code", String.valueOf(n));

        msg.setText("验证码:" + n);

        msg.setTo(email);//登录页面输入的 - 收件方

        //真正的发送
        mailSender.send(msg);

        return new Result("200", "邮箱发送成功");
    }

    @PostMapping("/login")
    @ResponseBody
    public Result login(HttpServletRequest request, String email, String code) {
        User u = userService.login(email);
        //u如果为空
        if (u == null)
            return new Result("404", "邮箱不存在!");
        if (null == code || code.trim().length() == 0)
            return new Result("400", "验证码不能为空!");
        HttpSession session = request.getSession();
        String sesCode = (String) session.getAttribute("code");
        //如果验证码放入到redis,可能是失效的.过期的
        if (null == sesCode)
            return new Result("500", "验证码过期了!");
        if (code.equals(sesCode)) {
            session.setAttribute("user", u);
            return new Result("200", "登录成功!");
        }
        return new Result("505", "验证码错误!");
    }

    @GetMapping("/login_enterprise")
    public String login_enterprise() {
        return "/WEB-INF/user/login/login_enterprise.jsp";
    }

    @Autowired
    private IMydeliveryService mydeliveryService;
    @Autowired
    private EmployeeMapper employeeMapper;
    @GetMapping("/mydelivery")
    public String mydelivery(HttpServletRequest request){
        HttpSession session =request.getSession();
        Employee employee = (Employee) session.getAttribute("employee");
        Integer userId =employeeMapper.findById(employee.getId()).getUserId();
        List<RecruitmentAndCompanyDTO> recruitmentAndCompanyDTOList = mydeliveryService.delivery(employee.getId());
        request.setAttribute("recruitmentAndCompanyDTOList",recruitmentAndCompanyDTOList);
        List<RecruitmentAndCompanyDTO> recruitmentAndCompanyDTOInterstList = mydeliveryService.deliveryInterested(employee.getId());
        request.setAttribute("recruitmentAndCompanyDTOInterstList",recruitmentAndCompanyDTOInterstList);
        List<RecruitmentAndCompanyDTO> recruitmentAndCompanyDTONoList = mydeliveryService.deliveryNo(employee.getId());
        request.setAttribute("recruitmentAndCompanyDTONoList",recruitmentAndCompanyDTONoList);
        List<RecruitmentAndCompanyDTO> recruitmentAndCompanyDTOYesList = mydeliveryService.deliveryYes(employee.getId());
        request.setAttribute("recruitmentAndCompanyDTOYesList",recruitmentAndCompanyDTOYesList);
        return "/WEB-INF/user/center/mydelivery.jsp";
    }

    @Autowired
    private IMycollectionService mycollectionService;

    @GetMapping("/mycollection")
    public String mycollection(HttpServletRequest request){
        HttpSession session=request.getSession();
        Employee employee= (Employee) session.getAttribute("employee");
        User user = userMapper.findById(employee.getUserId());
        List<EmployeeCollectionAndCompamyAndRecruitmentDTO> employeeCollectionAndCompamyAndRecruitmentDTOList=mycollectionService.collection(user.getId());
        request.setAttribute("employeeCollectionAndCompamyAndRecruitmentDTOList",employeeCollectionAndCompamyAndRecruitmentDTOList);
//        System.out.println(employeeCollectionAndCompamyAndRecruitmentDTOList);
        return "/WEB-INF/user/center/mycollection.jsp";
    }

    @Autowired
    private IResumeCenterService resumeCenterService;
    @Autowired
    private UserMapper userMapper;
    @GetMapping("/resume_center")
    public String resume_center(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Employee employee = (Employee) session.getAttribute("employee");
//        request.setAttribute("eid",employee.getId());
        User user = userMapper.findById(employeeMapper.findById(employee.getId()).getUserId());
        request.setAttribute("user",user);
        List<ResumeDTO> resumeDTOList = resumeCenterService.myResume(employee.getId());
        request.setAttribute("resumeDTOList", resumeDTOList);
        return "/WEB-INF/user/center/resume_center.jsp";
    }

    @Autowired
    private IMyblacklistService myblacklistService;

    @GetMapping("/myblacklist")
    public String myblacklist(HttpServletRequest request){
        HttpSession session=request.getSession();
        Employee employee= (Employee) session.getAttribute("employee");
//        System.out.println(employee.getId());
        request.setAttribute("eid",employee.getId());
        List<Company> companyList = myblacklistService.black(employee.getId());
//        System.out.println(companyList);
        request.setAttribute("companyList",companyList);
        return "/WEB-INF/user/center/myblacklist.jsp";
    }



    @GetMapping("/resume_view")
    public String resume_view(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Employee employee= (Employee) session.getAttribute("employee");
        request.setAttribute("employee",employee);
        User user = employeeInfoService.UserInfo(employee.getId());
        request.setAttribute("user",user);
//        System.out.println(user.getId());
        request.setAttribute("employee", employee);
//        System.out.println(employee);
        List<ResumeViewAndCompanyDTO> resumeViewList =resumeViewService.viewMe(employee.getId());
        request.setAttribute("resumeViewList",resumeViewList);
        request.setAttribute("count",resumeViewList.size());
        List<RecruitmentAndCompanyDTO> recruitmentAndCompanyDTOList = mydeliveryService.delivery(employee.getId());
        request.setAttribute("recruitmentAndCompanyDTOList",recruitmentAndCompanyDTOList);
        request.setAttribute("deliveryCount",recruitmentAndCompanyDTOList.size());
        return "/WEB-INF/user/center/resume_view.jsp";
    }

    @Autowired
    private IAccountSettingsService accountSettingsService;

    @GetMapping("/account_settings")
    public String account_setting(HttpServletRequest request){
        HttpSession session=request.getSession();
        Employee employee= (Employee) session.getAttribute("employee");
        User user = accountSettingsService.setting(employee.getId());
        request.setAttribute("user",user);
        return "/WEB-INF/user/center/account_settings.jsp";
    }
    @GetMapping("/password_change")
    public String passwordChange(HttpServletRequest request){
        HttpSession session=request.getSession();
        Employee employee= (Employee) session.getAttribute("employee");
        return "/WEB-INF/user/center/password_change.jsp";
    }


}
